package top.zk123.chain.handler;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import top.zk123.chain.bean.ReturnInfo;
import top.zk123.chain.exception.*;

/**
 * 自定义异常捕获处理
 *
 * @author Ke Zhang
 * @since 1.0
 */
@RestControllerAdvice
public class MyExceptionHandler {
    /**
     * 非授权访问 - 1001
     *
     * @return ReturnInfo
     */
    @ExceptionHandler(TokenInvalidException.class)
    public ReturnInfo TokenInvalidException() {
        return new ReturnInfo(1001, "非授权访问", null);
    }

    /**
     * 用户信息不存在 - 1002
     *
     * @return ReturnInfo
     */
    @ExceptionHandler(InvalidUserException.class)
    public ReturnInfo InvalidUserException() {
        return new ReturnInfo(1002, "用户信息不存在", null);
    }

    /**
     * 密码错误 - 1003
     *
     * @return ReturnInfo
     */
    @ExceptionHandler(WrongPasswordException.class)
    public ReturnInfo WrongPasswordException() {
        return new ReturnInfo(1003, "密码错误", null);
    }

    /**
     * 注册失败 - 1004
     *
     * @return ReturnInfo
     */
    @ExceptionHandler(RegisterFailedException.class)
    public ReturnInfo RegisterFailedException() {
        return new ReturnInfo(1004, "用户名被使用,注册失败", null);
    }

    /**
     * 文件上传失败 - 1005
     *
     * @return ReturnInfo
     */
    @ExceptionHandler(FileUploadException.class)
    public ReturnInfo FileUploadException() {
        return new ReturnInfo(1005, "文件上传失败", null);
    }

    /**
     * 上传文件为空 - 1006
     *
     * @return ReturnInfo
     */
    @ExceptionHandler(FileEmptyException.class)
    public ReturnInfo FileEmptyException() {
        return new ReturnInfo(1006, "上传文件不能为空", null);
    }

    /**
     * 上传文件为空 - 1007
     *
     * @return ReturnInfo
     */
    @ExceptionHandler(WrongFileTypeException.class)
    public ReturnInfo WrongFileTypeException() {
        return new ReturnInfo(1007, "不支持该类型的文件上传", null);
    }

    /**
     * 传入的属性值为空 - 1008
     *
     * @return ReturnInfo
     */
    @ExceptionHandler(NullPropertyException.class)
    public ReturnInfo NullPropertyException() {
        return new ReturnInfo(1008, "属性值不得为空", null);
    }

    /**
     * 指定 Excel 文件不存在 - 1009
     *
     * @return ReturnInfo
     */
    @ExceptionHandler(ExcelNotFoundException.class)
    public ReturnInfo ExcelNotFoundException() {
        return new ReturnInfo(1009, "指定Excel文件不存在", null);
    }

    /**
     * 解析Excel失败 - 1010
     *
     * @param e Exception
     * @return ReturnInfo
     */
    @ExceptionHandler(ExcelParseErrorException.class)
    public ReturnInfo ExcelParseErrorException(ExcelParseErrorException e) {
        return new ReturnInfo(1010, "解析Excel失败", e.getMessage());
    }

    /**
     * Excel图片名称与上传的图片不匹配 - 1011
     *
     * @param e Exception
     * @return ReturnInfo
     */
    @ExceptionHandler(ExcelDataLinkToPicFailedException.class)
    public ReturnInfo ExcelDataLinkToPicFailedException(ExcelDataLinkToPicFailedException e) {
        return new ReturnInfo(1011, "Excel图片名称与上传图片不匹配", e.getMessage());
    }

    /**
     * 文件后缀名为空 - 1012
     *
     * @return ReturnInfo
     */
    @ExceptionHandler(EmptyStringException.class)
    public ReturnInfo EmptyStringException() {
        return new ReturnInfo(1012, "getExtension() 空字符串!", null);
    }

    /**
     * 证书记录不存在 - 1013
     *
     * @return ReturnInfo
     */
    @ExceptionHandler(CertificateNotFoundException.class)
    public ReturnInfo CertificateNotFoundException() {
        return new ReturnInfo(1013, "证书不存在", null);
    }

    /**
     * OCR 识别身份证失败
     *
     * @return ReturnInfo
     */
    @ExceptionHandler(OCRFailedException.class)
    public ReturnInfo OCRFailedException() {
        return new ReturnInfo(1014, "OCR识别身份证失败", null);
    }

    /**
     * 身份证为假
     *
     * @return ReturnInfo
     */
    @ExceptionHandler(FakeIdCardException.class)
    public ReturnInfo FakeIdCardException() {
        return new ReturnInfo(1015, "身份证为假", null);
    }

    /**
     * 身份证已过期
     *
     * @return ReturnInfo
     */
    @ExceptionHandler(IdCardExpiredException.class)
    public ReturnInfo IdCardExpiredException() {
        return new ReturnInfo(1016, "身份证不在有效期内", null);
    }

    /**
     * 输入结果与OCR识别结果不同
     *
     * @return ReturnInfo
     */
    @ExceptionHandler(OCRInputDifferentException.class)
    public ReturnInfo OCRInputDifferentException() {
        return new ReturnInfo(1017, "输入结果与OCR识别结果不同", null);
    }

    /**
     * 实名制核验未通过
     *
     * @return ReturnInfo
     */
    @ExceptionHandler(UserVerifyFailed.class)
    public ReturnInfo UserVerifyFailed() {
        return new ReturnInfo(1018, "实名制核验未通过", null);
    }

    /**
     * 证书已被撤销，无法再次撤销
     *
     * @return ReturnInfo
     */
    @ExceptionHandler(CertRevokedException.class)
    public ReturnInfo CertRevokedException() {
        return new ReturnInfo(1019, "证书已被撤销，无法再次撤销", null);
    }

    /**
     * createActivityId 请求失败，系统繁忙
     *
     * @return ReturnInfo
     */
    @ExceptionHandler(WXSystemBusyException.class)
    public ReturnInfo WXSystemBusyException() {
        return new ReturnInfo(1020, "系统繁忙", null);
    }

    /**
     * createActivityId 请求失败，access_token 过期
     *
     * @return ReturnInfo
     */
    @ExceptionHandler(InvalidAccessTokenException.class)
    public ReturnInfo InvalidAccessTokenException() {
        return new ReturnInfo(1021, "access_token过期", null);
    }

    @ExceptionHandler(NoAuthenticationException.class)
    public ReturnInfo NoAuthenticationException() {
        return new ReturnInfo(1022, "未实名认证", null);
    }

    @ExceptionHandler(CertsExistOnChainException.class)
    public ReturnInfo CertsExistOnChainException() {
        return new ReturnInfo(1023, "证书已上链，请勿重复上链", null);
    }
}
